package com.pactera.mavenweb.entity;


import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

/**
 * 
 * @author will
 *
 */
@Entity  
@Table(name="t_user")
public class User {
	
	@Id
	@GeneratedValue(generator = "system-uuid")
	@GenericGenerator(name = "system-uuid", strategy = "uuid")
	@Column(name = "id", unique = true, nullable=false, length = 20)
//	@GeneOratedValue(strategy=GenerationType.AUTO)
	private int id;
	
	@Column(name = "name", length = 20, nullable=false)
	private String name;
	
	@Column(name = "password",length = 12, nullable=false)
	private String password;
	
	@Column(name = "phonenum",length = 32, nullable=false)
	private String phonenum;
	
	@Column(name = "enabled", nullable=true)
	private boolean enabled;
	
	@Column(name = "accountExpired")
	private boolean accountExpired;
	
	@Column(name = "credentialsExpired")
	private boolean credentialsExpired;
	
	@Column(name = "accountLocked")
	private boolean accountLocked;
	
	@Column(name = "salt", nullable=true, length = 25)
	private String salt;
	
//	@Column(name = "authority", nullable=true, length = 100)
	private Set<String> authority;
	
	private Set<UserRole> userRole = new HashSet<UserRole>();
	
	private Set<UserGroup> userGroup = new HashSet<UserGroup>();
	
	public User() {
		super();
	}
	
	public User(String name, 
			String password, String salt,
			String phonenum, boolean enabled, 
			boolean accountExpired, boolean credentialsExpired, 
			boolean accountLocked, Set<UserRole> userRole, 
			Set<UserGroup> userGroup) {
		this.name = name;
		this.password = password;
		this.enabled = enabled;
		this.accountExpired = accountExpired;
		this.credentialsExpired = credentialsExpired;
		this.accountLocked = accountLocked;
		this.userRole = userRole;
		this.userGroup = userGroup;
	}
	
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getPhonenum() {
		return phonenum;
	}
	public void setPhonenum(String phonenum) {
		this.phonenum = phonenum;
	}
	public boolean isEnabled() {
		return enabled;
	}
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	
	public boolean isAccountExpired() {
		return accountExpired;
	}

	public void setAccountExpired(boolean accountExpired) {
		this.accountExpired = accountExpired;
	}

	public boolean isCredentialsExpired() {
		return credentialsExpired;
	}

	public void setCredentialsExpired(boolean credentialsExpired) {
		this.credentialsExpired = credentialsExpired;
	}

	public boolean isAccountLocked() {
		return accountLocked;
	}

	public void setAccountLocked(boolean accountLocked) {
		this.accountLocked = accountLocked;
	}

	public String getSalt() {
		return salt;
	}
	
	public void setSalt(String salt) {
		this.salt = salt;
	}
	
	public void setAuthority(Set<String> authority) {
		this.authority = authority;
	}
	
	public Set<String> achieveAuthorities() {
		return this.authority;
	}
	
	//	@OneToMany(cascade = { CascadeType.REFRESH, CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE },mappedBy ="user")
	@OneToMany(cascade = { CascadeType.ALL }, mappedBy ="user", fetch = FetchType.LAZY)
	public Set<UserRole> getUserRole() {
		return userRole;
	}
	public void setUserRole(Set<UserRole> userRole) {
		this.userRole = userRole;
	}
	
	@OneToMany(cascade = { CascadeType.ALL }, mappedBy ="user", fetch = FetchType.LAZY)
	public Set<UserGroup> getUserGroup() {
		return userGroup;
	}
	public void setUserGroup(Set<UserGroup> userGroup) {
		this.userGroup = userGroup;
	}
}
